<?php

function zeigeAlleOrdnerAn($path){
	
	if( ($files = scandir($path)) == FALSE ){
		echo "<br>Ordner ".$path." nicht gefunden!\n";
		return;
	}   
	
	natsort($files);
	
    foreach ($files as $file) {
        if($file != '.' && $file != '..' && is_dir($path.'/'.$file)){
			echo "<div class='pictureDiv'>";
				echo "<a href=\"../picUpload/upload.php?folder=".$file."\"><img src='folder.png'></img></a>";
				echo "<h1 class='bildName'>".$file."</h1>";
			echo "</div>";	
        }
    }
	
	echo "<br>";
    
}

function zeigeAlleBilderAn($path, $folder){
	
	if( ($files = scandir($path)) == FALSE ){
		echo "<br>Ordner ".$path." nicht gefunden!\n";
		return;
	}   
	
	natsort($files);
	
	foreach ($files as $file) {
        if($file != '.' && $file != '..' && !is_dir($path.'/'.$file) && $file != 'loader.gif' && $file != 'close.png'){
				
			echo "<div class='pictureDiv'>";
				echo "<img src=".$path.'/'.$file."></img>";
				
				
				echo "<div class='picMenu'>";
				
				?>
				<form action="upload.php" id="deleteButton" method="post" onsubmit="return confirm('Wirklich L&ouml;schen?');"> 
					<input type="hidden" name="delete" value="<?php echo $file; ?>"><br>
					<input type="hidden" name="folder" value="<?php echo $folder; ?>">
					<input type="submit" value="Entfernen"> 
				</form>
				<?php
				
				echo "</div>";
				
				
				echo "<h1 class='bildName'>".$file."</h1>";
			echo "</div>";
        }
    }
  
}

function ordnerAnsicht($path, $folder){
	
	if($folder != ""){
		echo "<a class=\"backLink\" href='../picUpload/upload.php'>Zur&uuml;ck</a>";
		echo '<div id="currentFolder">'.$folder.'</div>';
	}
	else{
		echo "<a class=\"backLink\" href='../login.php'>Zur&uuml;ck</a>";
		echo '<div id="currentFolder">Galerie-Manager</div>';
	}
	
	?>
	<form action="../login.php" method="post">
				<input type="hidden" name="logout">
				<input type="submit" id="logoutButton" class="button" value="Logout">
	</form>
	<?php
	
	zeigeAlleOrdnerAn($path);
	zeigeAlleBilderAn($path, $folder);

	?>
	<br>
	<form id="uploadForm" action="upload.php" method="post" enctype="multipart/form-data"> 
	<input type="file" name="userfile">
	<input type="hidden" name="folder" value="<?php echo $folder; ?>">
	<input type="submit" value="Hochladen"> 
	</form>
	<?php
}

function deletePicWithName($delete){
	if( false !== unlink($delete) )
		return "Bild ".$delete." entfernt.<br>";
	else	
		return "Bild ".$delete." konnte nicht entfernt werden!<br>";
}
?>